package handler

import (
	"fmt"
	"gitee.com/simple-set/simple.io/src/process/socks"
	"gitee.com/simple-set/simple.proxy/src/config"
	"log/slog"
	"net"
)

type socksInterceptor struct {
	interceptor *Interceptor
}

func newSocksInterceptor() *socksInterceptor {
	if config.Config.Socks.AclFilePath != "" {
		return &socksInterceptor{interceptor: NewInterceptor(config.Config.Socks.AclFilePath)}
	}
	return &socksInterceptor{}
}

func (s *socksInterceptor) Intercept(request *socks.Request) bool {
	if s.interceptor == nil {
		return false
	}

	var reject bool
	if request.DstAddrType == socks.Domain {
		reject = s.interceptor.Intercept(string(request.DstAddr))
	} else {
		reject = s.interceptor.Intercept(net.IP(request.DstAddr).String())
	}
	if reject == true {
		slog.Warn(fmt.Sprintf("socks proxy access has been intercepted, %s -> %s", request.Session().Socket.RemoteAddr(), request.GetDstAddrString()))
	}
	return reject
}
